﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmHoaDonBanChiTiet : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private bool allowCloseForm = true;
        public static int hientrang = 0;
        private QuyDoiBOCollection quyDoiBOCollection = QuyDoiBOCollection.NewQuyDoiBOCollection();
        private HoaDonBanChiTietBOCollection HoaDonBanChiTietBOCollection = HoaDonBanChiTietBOCollection.NewHoaDonBanChiTietBOCollection();
        private HoaDonBanChiTietBOCollection hoaDonBanChiTietBOColl = HoaDonBanChiTietBOCollection.NewHoaDonBanChiTietBOCollection();
        private HoaDonBanBO HoaDonBanBO = HoaDonBanBO.NewRootHoaDonBanBO();
        #endregion variant

        //=====================================
        #region form
        public FrmHoaDonBanChiTiet()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmHoaDonChiTiet_Load);
            this.Shown += new EventHandler(FrmHoaDonChiTiet_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmHoaDonChiTiet_FormClosing);
            this.KeyDown += new KeyEventHandler(this.FrmHoadonnhap_KeyDown);

            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.btnChon.Click += new EventHandler(btnChon_Click);


            this.grdViewHoaDonBanChiTiet.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
            this.reTextMaVach.KeyDown += new KeyEventHandler(reTextMaVach_KeyDown);
                
        }


        private void FrmHoaDonChiTiet_Shown(object sender, EventArgs e)
        {
            this.lookUpEditKhachHang.Focus();
        }

        private void FrmHoaDonChiTiet_Load(object sender, EventArgs e)
        {
            //SetFormStatus(this.formStatus);

            dtNgayLapHoaDon.DateTime = Session.NgayHeThong;
            txtMaSo.Text = "BH-" + Session.NgayHeThong.ToString("dd/MM/yyyy") + "-" + DateTime.Now.ToString("HH:mm:ss");

            Utility.InitControl(this.reLookUpMatHang);
            Utility.InitControl(this.reLookUpDonViTinh);
            Utility.InitControl(this.reLookUpDMKhoID);

            Utility.InitControl(this.lookUpEditKhachHang);

            RefreshData();

            this.spinEditPhucVu.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditGiamGia.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditChietKhau.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditVAT.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditTienPhatSinh.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.grdViewHoaDonBanChiTiet.CellValueChanged += new CellValueChangedEventHandler(this.grdViewHoaDonBanChiTiet_CellValueChanged);

        }

        private void FrmHoaDonChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }

        private void FrmHoadonnhap_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyData == Keys.Delete)
            {
                int _idMatHang = StaticUtil.GetInt(this.grdViewHoaDonBanChiTiet.GetFocusedRowCellValue("MatHangID"), -1);
                int _idDonViTinh = StaticUtil.GetInt(this.grdViewHoaDonBanChiTiet.GetFocusedRowCellValue("DonViTinhID"), -1);
                if ((_idMatHang >= 0) & (_idDonViTinh >= 0))
                {
                    DeleteMatHang(_idMatHang, _idDonViTinh);
                }
                else
                {
                    MessageBox.Show("Bạn chưa chọn Vật tư để xóa! Vui lòng chọn lại", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.grdViewHoaDonBanChiTiet.Focus();
                }
            } 
            if ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10))
                this.Close();
            if ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5))
                RefreshData();
            if ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6))
                Save();
            if ((e.KeyCode == Keys.O && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F11))
                btnChon_Click(null,null);

        }
        #endregion form

        //=====================================
        #region control


        private void spinEditTienPhatSinh_ValueChanged(object sender, EventArgs e)
        {
            TinhTien();
        }
        #endregion control

        //=====================================
        #region button

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void btnChon_Click(object sender, EventArgs e)
        {
            FrmQuyDoi.quyDoiBOColl = QuyDoiBOCollection.NewQuyDoiBOCollection();
            FrmQuyDoi frm = new FrmQuyDoi(quyDoiBOCollection);
            frm.ShowDialog();
            if (FrmQuyDoi.quyDoiBOColl.Count > 0)
            {
                HoaDonBanChiTietBOCollection = TaoHoaDonBanQuyDoiChiTiet(hoaDonBanChiTietBOColl, FrmQuyDoi.quyDoiBOColl);

                sapxep();
                TinhTien();
            }
            else
            {
                HoaDonBanChiTietBOCollection = hoaDonBanChiTietBOColl;

                sapxep();
                TinhTien();
            }


            FrmQuyDoi.quyDoiBOColl = QuyDoiBOCollection.NewQuyDoiBOCollection();
        }

        #endregion button

        //=====================================
        #region grid
        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdViewHoaDonBanChiTiet_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                int _idMatHang = StaticUtil.GetInt(this.grdViewHoaDonBanChiTiet.GetFocusedRowCellValue("MatHangID"));
                int _idDonViTinh = StaticUtil.GetInt(this.grdViewHoaDonBanChiTiet.GetFocusedRowCellValue("DonViTinhID"));
                double _soLuong = StaticUtil.GetDouble(this.grdViewHoaDonBanChiTiet.GetFocusedRowCellValue("SoLuong"));
                double _giaBan = StaticUtil.GetDouble(this.grdViewHoaDonBanChiTiet.GetFocusedRowCellValue("DonGia"));

                int kiemtraID = this.HoaDonBanChiTietBOCollection.CheckExits(_idMatHang, _idDonViTinh);
                double thanhtien = (double)_soLuong * (double)_giaBan;

                HoaDonBanChiTietBOCollection[kiemtraID].SoLuong = _soLuong;
                HoaDonBanChiTietBOCollection[kiemtraID].ThanhTien = thanhtien;
                sapxep();
                TinhTien();
            }
            catch { }
        }
        #endregion grid

        //=====================================
        #region function

        private void RefreshData()
        {
            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reLookUpDonViTinh, donViTinhBOList, donViTinhBOList.Count);

            DanhMucBOList dmKhoBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DanhMucKho);
            Utility.SetData(this.reLookUpDMKhoID, dmKhoBOList, dmKhoBOList.Count);

            MatHangBOList matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.reLookUpMatHang, matHangBOList, matHangBOList.Count);

            KhachHangBOList khachHangBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.lookUpEditKhachHang, khachHangBOList, khachHangBOList.Count);

            #endregion BOList
            
            if (StaticUtil.GetInt(FrmPhieuYeuCau.PhieuYeuCauBO.PhieuYeuCauID.Value, -1) >= 0)
            {
                lookUpEditKhachHang.EditValue = StaticUtil.GetInt(FrmPhieuYeuCau.PhieuYeuCauBO.KhachHangID.Value, -1);
                txtNguoiMuaHang.Text = FrmPhieuYeuCau.PhieuYeuCauBO.NguoiYeuCau;
            }
            hoaDonBanChiTietBOColl = TaoHoaDonBanChiTiet(FrmPhieuYeuCau.PhieuYeuCauChiTietBOCollection);
            quyDoiBOCollection = TaoQuyDoi(hoaDonBanChiTietBOColl);
            HoaDonBanChiTietBOCollection = hoaDonBanChiTietBOColl;
            sapxep();
            TinhTien();
            
        }

        private void AddNew()
        {
            this.HoaDonBanBO = Model.HoaDonBanBO.NewRootHoaDonBanBO();
            this.HoaDonBanChiTietBOCollection = Model.HoaDonBanChiTietBOCollection.NewHoaDonBanChiTietBOCollection();
        }

        private void Save()
        {
            if (kiemtraLuu() == 1)
            {
                this.HoaDonBanBO.KhachHangID = StaticUtil.GetInt(lookUpEditKhachHang.EditValue);
                this.HoaDonBanBO.MaSo = txtMaSo.Text.Trim();
                this.HoaDonBanBO.NgayLapHoaDon = dtNgayLapHoaDon.DateTime;
                this.HoaDonBanBO.NoiDung = txtNoidung.Text.Trim();
                this.HoaDonBanBO.GhiChu = txtGhichu.Text.Trim();

                this.HoaDonBanBO.MucVAT = StaticUtil.GetDouble(spinEditVAT.Value);
                this.HoaDonBanBO.MucChietKhau = StaticUtil.GetDouble(spinEditChietKhau.Value);
                this.HoaDonBanBO.MucPhucVu = StaticUtil.GetDouble(spinEditPhucVu.Value);
                this.HoaDonBanBO.MucGiamGia = StaticUtil.GetDouble(spinEditGiamGia.Value);
                this.HoaDonBanBO.TienPhatSinh = StaticUtil.GetDouble(spinEditTienPhatSinh.Value);
                this.HoaDonBanBO.LyDoPhatSinh = txtLyDoPhatSinh.Text.Trim();

                this.HoaDonBanBO.TongSo = StaticUtil.GetDouble(spinEditTongSo.Value);
                this.HoaDonBanBO.ThanhTien = StaticUtil.GetDouble(spinEditThanhTien.Value);
                this.HoaDonBanBO.TienPhaiThu = StaticUtil.GetDouble(spinEditTongTien.Value);
                this.HoaDonBanBO.TienKhachTra = 0;
                this.HoaDonBanBO.BangChu = txtBangChu.Text.Trim();
                this.HoaDonBanBO.TienThua = this.HoaDonBanBO.TienKhachTra.Value - this.HoaDonBanBO.TienPhaiThu.Value;
                this.HoaDonBanBO.ThanhToan = 0;
                this.HoaDonBanBO.TinhTrangThanhToan = 0; this.HoaDonBanBO.TinhTrangXuatKho = 0;

                this.HoaDonBanBO.Save();

                int recordCount = this.HoaDonBanChiTietBOCollection.Count;
                if (recordCount > 0)
                {
                    for (int i = 0; i < recordCount; i++)
                    {
                        this.HoaDonBanChiTietBOCollection[i].HoaDonBanID = this.HoaDonBanBO.HoaDonBanID.Value;
                    }

                }

                this.HoaDonBanChiTietBOCollection = this.HoaDonBanChiTietBOCollection.Save();
                FrmPhieuYeuCau.PhieuYeuCauBO.ThucHien = 1;
                FrmPhieuYeuCau.PhieuYeuCauBO.Save();
                MessageBox.Show("Cập nhật thành công! ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                FrmPhieuYeuCau.daluu = 1;
                this.Close();
            }

        }

        private void sapxep()
        {

            int length = HoaDonBanChiTietBOCollection.Count;

            for (int i = 0; i < length; i++)
            {
                HoaDonBanChiTietBOCollection[i].STT0 = (i + 1);
            }

            bdSourceHoaDonBanChiTiet.DataSource = HoaDonBanChiTietBOCollection;
        }

        private void TinhTien()
        {
            double tongso = 0;
            double thanhtien = 0;
            double tongtien = 0;

            int length = HoaDonBanChiTietBOCollection.Count;

            for (int i = 0; i < length; i++)
            {
                tongso = tongso + HoaDonBanChiTietBOCollection[i].SoLuong.Value;
                thanhtien = thanhtien + HoaDonBanChiTietBOCollection[i].ThanhTien.Value;
            }

            spinEditTongSo.Value = (decimal)tongso;
            spinEditThanhTien.Value = (decimal)thanhtien;

            double mucVAT = StaticUtil.GetDouble(spinEditVAT.Value, 0);
            double tienVAT = thanhtien * mucVAT;
            spinEditTienVAT.Value = (decimal)tienVAT;
            double mucCK = StaticUtil.GetDouble(spinEditChietKhau.Value, 0);
            double tienCK = thanhtien * mucCK;
            spinEditTienChietKhau.Value = (decimal)tienCK;
            double mucGG = StaticUtil.GetDouble(spinEditGiamGia.Value, 0);
            double tienGG = thanhtien * mucGG;
            spinEditTienGiamGia.Value = (decimal)tienGG;
            double mucPV = StaticUtil.GetDouble(spinEditPhucVu.Value, 0);
            double tienPV = thanhtien * mucPV;
            spinEditTienPhucVu.Value = (decimal)tienPV;
            double TienPhatSinh = StaticUtil.GetDouble(spinEditTienPhatSinh.Value, 0);

            tongtien = thanhtien + tienVAT + TienPhatSinh - tienCK - tienGG + tienPV;
            spinEditTongTien.Value = (decimal)tongtien;
            txtBangChu.Text = VNCurrency.ToString(tongtien);
        }

        private void DeleteMatHang(int mathangID, int donvitinhID)
        {

            if (MessageBox.Show("Bạn có muốn xóa Mặt Hàng và Đơn vị tính này không? ", "Cảnh báo!!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                HoaDonBanChiTietBOCollection.Remove(mathangID, donvitinhID);
                int _idHoaDonBanChiTiet = StaticUtil.GetInt(this.grdViewHoaDonBanChiTiet.GetFocusedRowCellValue("HoaDonBanChiTietID"), -1);

                if (_idHoaDonBanChiTiet >= 0)
                {
                    HoaDonBanChiTietBO.DeleteHoaDonBanChiTietBO(_idHoaDonBanChiTiet);
                }
                sapxep();
                TinhTien();
            }
        }

        private int kiemtraLuu()
        {
            int tempkiemtra = 1;
            string strError = "";

            if (lookUpEditKhachHang.EditValue == null)
            {
                tempkiemtra = 0;
                strError = "Chưa chọn Khách hàng";
            }

            if (txtMaSo.Text.Trim() == "")
            {
                tempkiemtra = 0;
                strError = strError + " ;Chưa nhập Mã số";
            }

            if (tempkiemtra == 0)
                MessageBox.Show(strError, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return tempkiemtra;
        }

        private void reTextMaVach_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    int _idMatHang = StaticUtil.GetInt(this.grdViewHoaDonBanChiTiet.GetFocusedRowCellValue("MatHangID"));
                    int _idDonViTinh = StaticUtil.GetInt(this.grdViewHoaDonBanChiTiet.GetFocusedRowCellValue("DonViTinhID"));
                    int kiemtraID = this.HoaDonBanChiTietBOCollection.CheckExits(_idMatHang, _idDonViTinh);

                    FrmRMaVach._maVach = "";
                    FrmRMaVach._khoID = -1;
                    FrmRMaVach frm = new FrmRMaVach(_idMatHang, _idDonViTinh);
                    frm.ShowDialog();
                    HoaDonBanChiTietBOCollection[kiemtraID].MaVach = FrmRMaVach._maVach;
                    HoaDonBanChiTietBOCollection[kiemtraID].DanhMucKhoID = FrmRMaVach._khoID;
                    sapxep();
                    FrmRMaVach._maVach = "";
                    FrmRMaVach._khoID = -1;
                }
                catch { }
            }
        }

        private HoaDonBanChiTietBOCollection TaoHoaDonBanChiTiet(PhieuYeuCauChiTietBOCollection pyc)
        {
            Model.HoaDonBanChiTietBOCollection hoadonTemp = Model.HoaDonBanChiTietBOCollection.NewHoaDonBanChiTietBOCollection();
            
            int rowCount = pyc.Count;
            if (rowCount > 0)
            {
                for (int i = 0; i < rowCount; i++)
                {
                    int _matHangID = StaticUtil.GetInt(pyc[i].MatHangID.Value, -1);
                    int _donViTinhID = StaticUtil.GetInt(pyc[i].DonViTinhID.Value, -1);
                    double _soLuong = StaticUtil.GetDouble(pyc[i].SoLuong.Value, 0);

                    int vitri = hoadonTemp.CheckExits(_matHangID, _donViTinhID);
                    if (vitri < 0)
                    {
                        Model.HoaDonBanChiTietBO hdbBO = Model.HoaDonBanChiTietBO.NewRootHoaDonBanChiTietBO();
                        hdbBO.MatHangID = _matHangID;
                        hdbBO.TenHang = MatHangBO.GetRootMatHangBO(_matHangID).Ten;
                        hdbBO.DonViTinhID = _donViTinhID;
                        hdbBO.TenDonViTinh = DanhMucBO.GetRootDanhMucBO(_donViTinhID).NoiDung;
                        hdbBO.MaVach = "0";
                        hdbBO.SoLuong = _soLuong;
                        GiaBO giaBO = GiaBO.NewRootGiaBO();
                        giaBO = GiaBO.GetRootGiaBOByWhere("MatHangID = " + _matHangID.ToString() + " and DonViTinhID = " + _donViTinhID.ToString());

                        if (StaticUtil.GetInt(giaBO.GiaID, -1) < 0)
                            giaBO = GiaBO.NewRootGiaBO();

                        if (StaticUtil.GetInt(giaBO.GiaID, -1) >= 0)
                            hdbBO.DonGia = StaticUtil.GetDouble(giaBO.GiaBan, 0);
                        else
                            hdbBO.DonGia = 0;

                        hdbBO.ThanhTien = hdbBO.SoLuong.Value * hdbBO.DonGia.Value;

                        hoadonTemp.Add(hdbBO);
                    }
                    else
                    {
                        hoadonTemp[vitri].SoLuong = hoadonTemp[vitri].SoLuong.Value + _soLuong;
                        hoadonTemp[vitri].ThanhTien = hoadonTemp[vitri].SoLuong.Value * hoadonTemp[vitri].DonGia.Value;
                    }
                }
            }
            return hoadonTemp;
        }


        private QuyDoiBOCollection TaoQuyDoi(Model.HoaDonBanChiTietBOCollection hoadonTemp)
        {
            QuyDoiBOCollection tempQuyDoi = QuyDoiBOCollection.NewQuyDoiBOCollection();


            #region Lấy list các IDMatHang được quy đổi...he he
            
            string listID = "";
            int recordCount = hoadonTemp.Count;
            if (recordCount > 0)
            {
                for (int i = 0; i < recordCount; i++)
                {
                    object _idVattu = hoadonTemp[i].MatHangID;
                    listID += _idVattu.ToString() + ",";
                }
            }

            if (listID != "")
            {
                string strWhere = " MatHang2ID in ('" + listID.Replace(",", "','") + "')";
                tempQuyDoi =
                    QuyDoiBOCollection.GetQuyDoiBOCollection(strWhere);
            }
            #endregion

            return tempQuyDoi;
        }


        private HoaDonBanChiTietBOCollection TaoHoaDonBanQuyDoiChiTiet(HoaDonBanChiTietBOCollection hoadonTemp, QuyDoiBOCollection quydoi)
        {
            Model.HoaDonBanChiTietBOCollection hoadon = Model.HoaDonBanChiTietBOCollection.NewHoaDonBanChiTietBOCollection();

            if (quydoi.Count <= 0)
                hoadon = hoadonTemp;
            else
            {
                int sohang = hoadonTemp.Count;
                if (sohang > 0)
                    for (int j = 0; j < sohang; j++)
                    {
                        int _matHangID = StaticUtil.GetInt(hoadonTemp[j].MatHangID, -1);
                        int _donViTinhID = StaticUtil.GetInt(hoadonTemp[j].DonViTinhID, -1);
                        double _soLuong = StaticUtil.GetDouble(hoadonTemp[j].SoLuong, -1);
                        QuyDoiBO quyDoiBO = quydoi.GetQuyDoiBO(_matHangID, _donViTinhID);

                        if (StaticUtil.GetInt(quyDoiBO.QuyDoiID.Value, -1) < 0)
                        {
                            int vihientai = hoadon.CheckExits(_matHangID, _donViTinhID);
                            if (vihientai < 0)
                            {
                                Model.HoaDonBanChiTietBO hdbBO = hoadonTemp[j];
                                hoadon.Add(hdbBO);
                            }
                            else
                            {
                                hoadon[vihientai].SoLuong = hoadon[vihientai].SoLuong.Value + hoadonTemp[j].SoLuong.Value;
                                hoadon[vihientai].ThanhTien = hoadon[vihientai].DonGia.Value * hoadon[vihientai].SoLuong.Value;
                            }
                        }
                        else
                        {
                            double _hesoQuydoi = StaticUtil.GetDouble(quyDoiBO.HeSoQuyDoi, 1);
                            double _soluong1 = 0;
                            double _soluong2 = 0;
                            _soluong1 = _soLuong / _hesoQuydoi;

                            string catgian = _soluong1.ToString();
                            int vitriphay = catgian.IndexOf(',');
                            if (vitriphay > -1)
                            {
                                string sohangchuc = catgian.Substring(0, vitriphay);
                                _soluong1 = StaticUtil.GetDouble(sohangchuc, 0);
                            }

                            _soluong2 = _soLuong - _soluong1 * _hesoQuydoi;

                            if (_soluong1 <= 0)
                            {
                                int vihientai = hoadon.CheckExits(_matHangID, _donViTinhID);
                                if (vihientai < 0)
                                {
                                    Model.HoaDonBanChiTietBO hdbBO = hoadonTemp[j];
                                    hoadon.Add(hdbBO);
                                }
                                else
                                {
                                    hoadon[vihientai].SoLuong = hoadon[vihientai].SoLuong.Value + hoadonTemp[j].SoLuong.Value;
                                    hoadon[vihientai].ThanhTien = hoadon[vihientai].DonGia.Value * hoadon[vihientai].SoLuong.Value;
                                }
                            }
                            else
                            {
                                int vihientaiID2 = hoadon.CheckExits(_matHangID, StaticUtil.GetInt(quyDoiBO.DonViTinh1ID, -1));
                                if (vihientaiID2 < 0)
                                {
                                    Model.HoaDonBanChiTietBO hdbBO = Model.HoaDonBanChiTietBO.NewRootHoaDonBanChiTietBO();
                                    hdbBO.MatHangID = StaticUtil.GetInt(quyDoiBO.MatHangID, -1);
                                    hdbBO.TenHang = MatHangBO.GetRootMatHangBO(StaticUtil.GetInt(hdbBO.MatHangID, -1)).Ten;
                                    hdbBO.DonViTinhID = StaticUtil.GetInt(quyDoiBO.DonViTinh1ID, -1);
                                    hdbBO.TenDonViTinh = DanhMucBO.GetRootDanhMucBO(StaticUtil.GetInt(quyDoiBO.DonViTinh1ID, -1)).NoiDung;
                                    hdbBO.MaVach = "0";
                                    hdbBO.SoLuong = _soluong1;
                                    GiaBO giaBO = GiaBO.NewRootGiaBO();
                                    giaBO = GiaBO.GetRootGiaBOByWhere("MatHangID = " + StaticUtil.GetInt(quyDoiBO.MatHangID, -1).ToString() + " and DonViTinhID = " + StaticUtil.GetInt(quyDoiBO.DonViTinh1ID, -1).ToString());

                                    if (StaticUtil.GetInt(giaBO.GiaID, -1) < 0)
                                        giaBO = GiaBO.NewRootGiaBO();

                                    if (StaticUtil.GetInt(giaBO.GiaID, -1) >= 0)
                                        hdbBO.DonGia = StaticUtil.GetDouble(giaBO.GiaBan, 0);
                                    else
                                        hdbBO.DonGia = 0;

                                    hdbBO.ThanhTien = hdbBO.SoLuong.Value * hdbBO.DonGia.Value;

                                    hoadon.Add(hdbBO);

                                    if (_soluong2 > 0)
                                    {
                                        int vihientaile = hoadon.CheckExits(_matHangID, _donViTinhID);
                                        if (vihientaile < 0)
                                        {
                                            Model.HoaDonBanChiTietBO hdbBO1 = Model.HoaDonBanChiTietBO.NewRootHoaDonBanChiTietBO();
                                            hdbBO1.MatHangID = _matHangID;
                                            hdbBO1.TenHang = MatHangBO.GetRootMatHangBO(_matHangID).Ten;
                                            hdbBO1.DonViTinhID = _donViTinhID;
                                            hdbBO1.TenDonViTinh = DanhMucBO.GetRootDanhMucBO(_donViTinhID).NoiDung;

                                            hdbBO1.MaVach = "0";
                                            hdbBO1.SoLuong = _soluong2;
                                            GiaBO giaBO1 = GiaBO.NewRootGiaBO();
                                            giaBO1 = GiaBO.GetRootGiaBOByWhere("MatHangID = " + _matHangID.ToString() + " and DonViTinhID = " + _donViTinhID.ToString());

                                            if (StaticUtil.GetInt(giaBO1.GiaID, -1) < 0)
                                                giaBO1 = GiaBO.NewRootGiaBO();

                                            if (StaticUtil.GetInt(giaBO1.GiaID, -1) >= 0)
                                                hdbBO1.DonGia = StaticUtil.GetDouble(giaBO1.GiaBan, 0);
                                            else
                                                hdbBO1.DonGia = 0;

                                            hdbBO1.ThanhTien = hdbBO1.SoLuong.Value * hdbBO1.DonGia.Value;

                                            hoadon.Add(hdbBO1);


                                        }
                                        else
                                        {
                                            hoadon[vihientaile].SoLuong = hoadon[vihientaile].SoLuong.Value + _soluong2;
                                            hoadon[vihientaile].ThanhTien = hoadon[vihientaile].DonGia.Value * hoadon[vihientaile].SoLuong.Value;
                                        }
                                    }
                                }

                                else
                                {
                                    hoadon[vihientaiID2].SoLuong = hoadon[vihientaiID2].SoLuong.Value + _soluong1;
                                    hoadon[vihientaiID2].ThanhTien = hoadon[vihientaiID2].DonGia.Value * hoadon[vihientaiID2].SoLuong.Value;
                                }
                            }
                        }
                    }
            }
            return hoadon;
        }

        #endregion function

    }
}